
#include "ring_buffer.h"

uint16 ring_buffer_read ( ring_buffer_def *ring_buf , uint8 *buf , uint16 length )
{
    uint8 i=0;
    
	while ( length-- ){
        if ( ring_buf->pos_read != ring_buf->pos_write ){
            buf [i++] = ring_buf->buf[ ring_buf->pos_read++ ];
            if ( ring_buf->pos_read >= ring_buf->buf_size )
                ring_buf->pos_read = 0;
        }
        else
            break;
    }
	
    return i;
}

uint16 ring_buffer_write ( ring_buffer_def *ring_buf , uint8 *buf , uint16 length )
{
    uint8 i=0;
	
    while ( length -- )
	{
        if ( ( (ring_buf->pos_write+1) % ring_buf->buf_size ) != ring_buf->pos_read )
		{
            ring_buf->buf[ring_buf->pos_write++] = buf[i++];
        }
		else
		{
            break;
        }
		ring_buf->pos_write %= ring_buf->buf_size ;
    }
	
    return i;
}
